package org.lgiavedoni.android.franq.interceptor.phone.sms;

import org.lgiavedoni.android.franq.core.contact.Contact;
import org.lgiavedoni.android.franq.core.message.MessageIn;
import org.lgiavedoni.android.franq.core.message.MessageInterceptor;
import org.lgiavedoni.android.franq.core.question.imp.ContactQuestionWithPhoneNumber;
import org.lgiavedoni.android.franq.core.question.imp.TextQuestion;
import org.lgiavedoni.android.franq.core.speak.CommonPhraseHelper;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.phone.sms.SMSService;

public class SMSMessageInterceptor extends MessageInterceptor {

	@Override
	public void processMessage(MessageIn message) throws Exception {

		// answer
		Contact to = makeContactQuestion(new ContactQuestionWithPhoneNumber("To?"));
		if (to == null) {
			// The questions was Discarded
			CommunicationService.inst.sendMessage(CommonPhraseHelper.DISCARDED_QUESTION);
			return;
		}

		String messageText = makeTextQuestion(new TextQuestion("[" + to + "], Message?"));
		if (messageText != null) {
			SMSService.inst.sendSMS(to.getSMSPhone(), messageText);
			CommunicationService.inst.sendMessage(CommonPhraseHelper.DONE);
		}
	}

	@Override
	public void fail() {
		CommunicationService.inst.sendDefaultTextMessage("Ups, I get a error trying to send your message");
	}

	@Override
	public String[] getInterceptorTags() {
		return new String[] { "sms", "message", "text message" };
	}

}
